﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MPosters.ViewModels;
using System.Web.Helpers;
using System.IO;

namespace MPosters.Controllers
{
    public class WebImageController : Controller
    {
        //
        // GET: /Image/

        public ActionResult Index()
        {
            ProfileViewModel objPVM = new ProfileViewModel();
            return View(objPVM);
        }

        [HttpPost]
        public ActionResult Upload(ProfileViewModel model)
        {
            var image = WebImage.GetImageFromRequest();

            if (image != null)
            {
                if (image.Width > 500)
                {
                    image.Resize(500, ((500 * image.Height) / image.Width));
                }

                var filename = Path.GetFileName(image.FileName);
                image.Save(Path.Combine("../Uploads/Images", filename));
                filename = Path.Combine("~/Uploads/Images", filename);

                model.ImageUrl = Url.Content(filename);

            }

            return View("Index", model);
        }

    }
}
